package net.toby200.canalbuddy.notices.text;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import net.toby200.canalbuddy.notices.Event;
import net.toby200.canalbuddy.notices.model.Notice;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import de.greenrobot.event.EventBus;

/**
 * Created by toby on 20/06/2014.
 */
public class NoticesListFragment extends ListFragment {
    private static final Logger LOG = Logger.getLogger(NoticesListFragment.class.getName());

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        LOG.info("listFragment onActivityCreated");
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onAttach(Activity activity) {
        LOG.info("onAttach");
        super.onAttach(activity);
        if (getListAdapter() == null) {
            LOG.info("Setting notice list adapter");
            setListAdapter(new NoticeArrayAdapter(activity.getApplicationContext(), new ArrayList<Notice>()));
        }

    }

    @Override
    public void onDetach() {
        LOG.info("onDetach, removing activity selectionListener");
        super.onDetach();

    }

    @Override
    public void onDestroy() {
        LOG.info("NoticesListFragment destroyed");
        super.onDestroy();

    }

    @Override
    public void onListItemClick(ListView lv, View view, int position, long id) {
        Notice notice = (Notice) lv.getItemAtPosition(position);

        LOG.log(Level.INFO, "Item selected: " + notice.getTitle() + ", posting NoticeSelectedEvent to event bus");
        EventBus.getDefault().post(new Event.NoticeSelectedEvent(notice));
    }
}
